package com.dayaokeji.rhythmschoolstudent.client.home.meeting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ActivityStackManager;
import com.dayaokeji.rhythmschoolstudent.client.common.ListFragment;
import com.dayaokeji.rhythmschoolstudent.client.home.meeting.adapter.ChooseMeetingRoomAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.api.MeetingApi;
import com.dayaokeji.server_api.domain.MeetingInfo;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import retrofit2.Call;

/**
 * Created by Gorden on 2017/10/26.
 */

public class ChooseMeetingRoomListFragment extends ListFragment<MeetingInfo,BaseViewHolder> {

    public static final String EXTRA_MEETING_INFO = "meeting_info";

    public static ChooseMeetingRoomListFragment newInstance() {
        
        Bundle args = new Bundle();
        
        ChooseMeetingRoomListFragment fragment = new ChooseMeetingRoomListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setAdapter(new ChooseMeetingRoomAdapter());

        getRecyclerView().addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .marginResId(R.dimen.default_divider_height)
                .build());

        if (getAdapter() != null) {
            getAdapter().setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    MeetingInfo meetingInfo = (MeetingInfo) adapter.getData().get(position);
                    if (getActivity() != null) {
                        Intent intent = getActivity().getIntent();
                        intent.putExtra(EXTRA_MEETING_INFO,meetingInfo);
                        getActivity().setResult(Activity.RESULT_OK,intent);
                        ActivityStackManager.finish(getActivity());
                    }
                }
            });
        }

    }


    @Override
    protected Call createCall(int offset, int max) {
        return ApiUtils.getApi(MeetingApi.class).meetingInfoList(String.valueOf(UserUtils.getUserInfo().getUniversityId()),offset,max);
    }
}
